from Foo.capability_json import getDriver
from Foo.login import Login
from Foo.pageSwitch import PageSwitch
from util.baseView import BaseView
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
import logging
import logging.config
from faker import Faker

import time

'''
desc：HD091校园试点,单机人员新增和采集功能
auth：shiwen
date:2020-12-09
'''

class Person(BaseView):
    CON_LOG = '../docs/log.conf'
    logging.config.fileConfig(CON_LOG)
    f = Faker(locale='zh_CN')
    count = 0

    def __init__(self, driver):
        BaseView.__init__(self, driver)
        self.image_icon = (By.ID, 'com.sykean.biometrics:id/imageView_face_icon')
        self.edit = (By.ID, 'com.sykean.biometrics:id/modify')  # 人员编辑按钮
        self.iris_capture = (By.ID, 'com.sykean.biometrics:id/btn_iris_capture')  # 虹膜采集
        self.face_capture = (By.ID, 'com.sykean.biometrics:id/btn_face_capture')
        self.dialog_ok = (By.ID, 'com.sykean.biometrics:id/btn_dialog_ok')  # 确认是否重新采集虹膜
        self.dialog_cancel = (By.ID, 'com.sykean.biometrics:id/btn_dialog_cancel')  # 确认是否取消本次操作
        self.back = (By.ID, 'com.sykean.biometrics:id/btn_back')
        self.add_user = (By.ID,'com.sykean.biometrics:id/ibtn_add_user')
        # self.edit_values = (By.ID,'com.sykean.biometrics:id/edt_edit_value')
        self.save_user = (By.ID,'com.sykean.biometrics:id/btn_save_user')
        self.home = (By.ID,'com.sykean.biometrics:id/btn_home')
    def test_edit_person(self):
        el = self.find_element(*self.image_icon)
        # 长按
        TouchAction(self.driver).long_press(el).perform()
        time.sleep(2)
        self.click_element(*self.edit)
    def test_add_user(self):

        # 人员新增按钮
        self.click_element(*self.add_user)

        eles = self.driver.find_elements_by_class_name("android.widget.EditText")
        # 编号
        eles[0].send_keys(self.f.random_int())
        # 姓名
        eles[1].send_keys(self.f.name())
        # 身份证号
        eles[2].send_keys(self.f.ssn())
        # 卡号
        # eles[3].send_keys(self.f.phone_number())
        # 部门
        eles[3].send_keys(self.f.company_prefix())
        # 韦根
        eles[5].send_keys(self.f.phone_number())
        # 传参count,需要使用全局变量count
        self.test_regist()

        self.click_element(*self.save_user)
        # logging.info('---人员新增成功---')

    def test_regist(self):
        # 特征采集
        try:
            self.click_element(*self.iris_capture)
            logging.info('--第{}次采集开始---'.format(self.get_count()))
            time.sleep(5)
            self.click_element(*self.back)
        except Exception as e:
            logging.info('---生物特征采集异常---',e.args)

    def test_back(self):
        self.click_element(*self.back)
    def test_home(self):
        self.click_element(*self.home)

    '''
    desc:使用类方法，每一次实例化类后,count不会初始化，仍会加载上次被修改后的的值
         这里再每一次调用注册生物特征方法后，count+1实现记录采集次数
    '''
    @classmethod
    def get_count(cls):
        cls.count += 1
        return cls.count


if __name__ == '__main__':

    driver = getDriver()

    for i in range(10000):
        Login(driver).login()
        # 进入人员管理模块
        PageSwitch(driver).menuUser()
        p = Person(driver)

        logging.info('-----循环第{0}次,i%2={1}-----'.format(i,i%2))
        if i%2 !=0:
            p.test_home()
            logging.info('-----切换至识别模式-----')
            time.sleep(3)
        else:
            try:
                logging.info('-----p.count={}'.format(p.count))
                logging.info('----进入采集模式-----')
                p.test_add_user()
                logging.info('-----采集完成-----')
                logging.info('------点击home按键-----')
                p.test_home()
                logging.info('------home按键已执行-----')

            except:
                print('---异常终止，请继续---')
                try:
                    p.test_home()
                except:
                    p.test_back()
                    p.test_home()












